# cryptography的使用
import os
import requests,re
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend

class AESCrypto(object):

    AES_CBC_KEY = "tmkj2018haerbin1"
    AES_CBC_IV = "0912028303740465"

    @classmethod
    def encrypt(cls, data, mode='cbc'):
        func_name = '{}_encrypt'.format(mode)
        func = getattr(cls, func_name)
        return func(data)

    @classmethod
    def decrypt(cls, data, mode='cbc'):
        func_name = '{}_decrypt'.format(mode)
        func = getattr(cls, func_name)
        return func(data)

    @staticmethod
    def pkcs7_padding(data):
        if not isinstance(data, bytes):
            data = data.encode()

        padder = padding.PKCS7(algorithms.AES.block_size).padder()

        padded_data = padder.update(data) + padder.finalize()

        return padded_data

    @classmethod
    def cbc_encrypt(cls, data):
        if not isinstance(data, bytes):
            data = data.encode()

        cipher = Cipher(algorithms.AES(cls.AES_CBC_KEY),
                        modes.CBC(cls.AES_CBC_IV),
                        backend=default_backend())
        encryptor = cipher.encryptor()

        padded_data = encryptor.update(cls.pkcs7_padding(data))

        return padded_data

    @classmethod
    def cbc_decrypt(cls, data):
        if not isinstance(data, bytes):
            data = data.encode()
        if type(cls.AES_CBC_KEY) == str:
            cls.AES_CBC_KEY = bytes(cls.AES_CBC_KEY, 'utf-8')
        print(cls.AES_CBC_KEY)
        if type(cls.AES_CBC_IV) == str:
            cls.AES_CBC_IV = bytes(cls.AES_CBC_IV, 'utf-8')
        cipher = Cipher(algorithms.AES(cls.AES_CBC_KEY),
                        modes.CBC(cls.AES_CBC_IV),
                        backend=default_backend())
        decryptor = cipher.decryptor()

        uppaded_data = cls.pkcs7_unpadding(decryptor.update(data)+ decryptor.finalize())

        uppaded_data = uppaded_data.decode()
        return uppaded_data

    @staticmethod
    def pkcs7_unpadding(padded_data):
        unpadder = padding.PKCS7(128).unpadder()
        data = unpadder.update(padded_data)

        try:
            uppadded_data = data + unpadder.finalize()
        except ValueError:
            raise Exception('无效的加密信息!')
        else:
            return uppadded_data
url = "http://harbinbus.com:10208/WifiBusInterface/transfer/bus!WaitBusLine.action?lineNo=Z47&isUpDown=1"
res = requests.get(url)
# test = "aaa bbb"
newKey = re.sub('\s*', "", res.text)
print(newKey)
dec = AESCrypto.decrypt(newKey[16:])
print(dec)